package com.app.ryjy.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class JsonFactory {
    public static ObjectMapper DEFAULT_MAPPER;

    private JsonFactory() {
    }

    public static ObjectMapper getMapper() {
        return DEFAULT_MAPPER;
    }

    public static String bean2json(Object bean) {
        if (bean == null) {
            return "";
        } else if (bean instanceof String) {
            return (String)bean;
        } else {
            try {
                return DEFAULT_MAPPER.writeValueAsString(bean);
            } catch (Exception var2) {
                throw new RuntimeException(var2);
            }
        }
    }

    public static <T> T json2bean(String json, Class<T> clazz) {
        if (json == null) {
            return null;
        } else {
            try {
                return DEFAULT_MAPPER.readValue(json, clazz);
            } catch (Exception var3) {
                throw new RuntimeException(var3);
            }
        }
    }

    public static <T> T json2bean(String json, TypeReference<T> typeReference) {
        if (json == null) {
            return null;
        } else {
            try {
                return DEFAULT_MAPPER.readValue(json, typeReference);
            } catch (Exception var3) {
                throw new RuntimeException(var3);
            }
        }
    }

    public static String getJson(String data) {
        if (data != null && !data.isEmpty()) {
            try {
                String json = DEFAULT_MAPPER.writeValueAsString(data);
                return json != null && json.length() > 0 ? json.substring(1, json.length() - 1) : "";
            } catch (Exception var2) {
                throw new RuntimeException(var2);
            }
        } else {
            return data;
        }
    }

    public static String toJson(String data) {
        if (data != null && !data.isEmpty()) {
            String reDate = StringUtils.replace(data, "\\", "\\\\");
            reDate = StringUtils.replace(reDate, "\r", "\\\\r");
            reDate = StringUtils.replace(reDate, "\t", "\\\\t");
            reDate = StringUtils.replace(reDate, "\b", "\\\\b");
            reDate = StringUtils.replace(reDate, "\f", "\\\\f");
            reDate = StringUtils.replace(reDate, "\n", "\\\\n");
            reDate = StringUtils.replace(reDate, "\"", "\\\"");
            return reDate;
        } else {
            return data;
        }
    }

    static {
        DEFAULT_MAPPER = (new ObjectMapper()).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false).configure(Feature.ALLOW_SINGLE_QUOTES, true).setSerializationInclusion(Include.NON_NULL).setTimeZone(TimeZone.getTimeZone("GMT+8:00")).setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    public static class TypeReference<T> extends com.fasterxml.jackson.core.type.TypeReference<T> {
        public TypeReference() {
        }
    }
}
